package com.cmrx.model.controller;

import com.cmrx.model.entity.CommonPicture;
import com.cmrx.model.entity.Photo;
import com.cmrx.model.entity.User;
import com.cmrx.model.service.ScenePhotoService;
import com.cmrx.model.util.JwtUtil;
import com.cmrx.model.util.ResultVoUtil;
import com.cmrx.model.vo.IRespCode;
import com.cmrx.model.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by angle on 2019/2/16.
 */

@RestController
@RequestMapping("scenephoto")
@Slf4j
@Api(value = "/scenephoto",description = "现场照片接口类")
public class ScenePhotoController {
    @Autowired
    private ScenePhotoService scenePhotoService;

    /**
     * 添加现场照片
     * @param map
     * @return
     */
    @RequestMapping(value = "/upPhoto",method = RequestMethod.POST)
    @ApiOperation(value = "添加现场照片接口")
    public ResultVo upload(@RequestBody Map<String,String> map){
        String id = null;
        String message = "";
        //获取用户
        User user = null;
        try {
            Photo photo = new Photo();
            CommonPicture commonPicture = new CommonPicture();
            String token = map.get("token");
            user = JwtUtil.deToken(token);
            photo.setPhotoType(map.get("photoType"));
            commonPicture.setType(map.get("type"));
            commonPicture.setDescription(map.get("description"));
            commonPicture.setInvestigationId(map.get("investigationId"));
            photo.setInvestigationId(map.get("investigationId"));
            commonPicture.setContent(Base64.getDecoder().decode(map.get("content").replaceAll("\n","").replaceAll("\r","")));
            commonPicture.setWidth(map.get("width"));
            commonPicture.setHeigth(map.get("height"));
            commonPicture.setFileName(map.get("fileName"));

            if (token == null || "".equals(token) || IRespCode.user_ERR_msg.equals(token)) {
                return ResultVoUtil.error(IRespCode.user_ERR_CODE, IRespCode.user_ERR_msg);
            }
            if (photo.getPhotoType() == null || "".equals(photo.getPhotoType())) {
                return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE, "图片类型不能为空");
            }
            if (commonPicture.getType() == null || "".equals(commonPicture.getType())) {
                return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE, "图片后缀类型不能为空");
            }
            if (photo.getInvestigationId() == null || "".equals(photo.getInvestigationId())) {
                return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE, "现勘号不能为空");
            }
            if (commonPicture.getContent() == null || "".equals(commonPicture.getContent())) {
                return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE, "图片不能为空");
            }
            if (commonPicture.getHeigth() == null || "".equals(commonPicture.getHeigth())) {
                ResultVoUtil.error(IRespCode.SERVER_ERR_CODE, "图片高度不能为空");
            }
            if (commonPicture.getWidth() == null || "".equals(commonPicture.getWidth())) {
                ResultVoUtil.error(IRespCode.SERVER_ERR_CODE, "图片宽度不能为空");
            }
            commonPicture.setCategory("2");
            // id === 条目id
            id = scenePhotoService.addPhoto(photo, user, commonPicture);

        }catch (Exception e){
            return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE,"添加失败");
        }

        if(id!=null && !"".equals(id)) {
           return ResultVoUtil.success(id);
        }else{
            return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE,"添加失败");
        }
    }


    /**
     * 获取现场照片
     * @param map
     * @return
     */
    @RequestMapping(value = "getPhoto",method = RequestMethod.POST)
    @ApiOperation(value = "获取现场照片接口")
    public ResultVo getPhoto(@RequestBody Map<String,String> map) {
        String investigationId = "";
        if(map.containsKey("investigationId")){
            investigationId = map.get("investigationId");
        }
        if(investigationId==null || "".equals(investigationId)){
            return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE,"现勘号不能为空");
        }
        List<Photo> list = scenePhotoService.queryByInvestigationId(investigationId);
        return ResultVoUtil.success(list);
    }


    /**
     * 删除现场照片
     * @param map
     * @return
     */
    @RequestMapping(value = "deletePhoto",method = RequestMethod.POST)
    @ApiOperation(value = "删除现场照片接口")
    public ResultVo deletePhoto(@RequestBody Map<String,String> map) {
        String id = "";
        String token = "";
        String message = "";
        if(map.containsKey("id")){
            id = map.get("id");
        }
        if(id==null || "".equals(id)){
            return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE,"主键id不能为空");
        }
        //token信息
        if(map.containsKey("token")){
            token = map.get("token");
        }
        if(token==null || "".equals(token)){
            return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE,"用户未登录");
        }
        //登录用户
        User user = JwtUtil.deToken(token);

        int count = scenePhotoService.deleteById(id);


        if(count>0) {
            return ResultVoUtil.success("成功");
        }else{
            return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE,"删除失败");
        }
    }


    /**
     * 修改现场照片
     * @param map
     * @return
     */
    @RequestMapping(value = "updatePhoto",method = RequestMethod.POST)
    @ApiOperation(value = "修改现场照片接口")
    public ResultVo updatePhoto(@RequestBody Map<String,String> map) {
        Photo photo = new Photo();
        if(StringUtils.isEmpty(map.get("id"))){
            return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE, "主键id不能为空");
        }
        photo.setSerialNo(map.get("index"));
        photo.setId(map.get("id"));
        photo.setCreateUser(map.get("createUser"));
        photo.setUpdateUser(map.get("updateUser"));
        photo.setPhotoType(map.get("photoType"));
        try {
            int count = scenePhotoService.updateById(photo);
            if(count>0) {
                return ResultVoUtil.success("成功");
            }else{
                return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultVoUtil.error(IRespCode.SERVER_ERR_CODE,"修改失败");
        }
    }

}
